<template>
	<om-formsub-outer :index="index">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue">
		</om-autoform>
	</om-formsub-outer>
</template>

<script>
import { subOnLoad, changeEditor, configSelectData } from '../../../static/js/formUtil.js';
import { ignoreHtml, ptFloat, minusF, getDateStr, joinObjStr,confirmModal } from '@/static/js/common.js';
import urlUtil from "@/static/js/urlUtil.js"
export default {
	provide() {
		return {
			formPage: this
		};
	},
	data() {
		return {
			initValue: {},
			autoformdata: [],
			cachename: '',
			index: null,
			datas: [],
			order: 0,
			formdata: {},
			relevantAbilityCacheKey: 'hr-train-trainYearPlan-title2-relevantAbility-cache',
			postCacheKey: 'hr-train-trainYearPlan-title2-post-cache',
			projectCacheKey:"hr-train-trainYearPlan-title2-project-cache",
			trainTypeCacheKey:"hr-train-trainYearPlan-title2-trainType-cache",
			evaluateUserPostCacheKey:'hr-train-trainYearPlan-title2-evaluateUserPost-cache',
			monthCacheKey:"hr-train-trainYearPlan-title2-monthCacheKey-cache",
			monthCacheKey1:"hr-train-trainYearPlan-title2-monthCacheKey1-cache",
			relationCacheKey:'hr-train-trainYearPlan-title2-relation-cache',
			teacherCacheKey:"hr-train-trainYearPlan-title2-teacher-cache",
			relationList:[],
			startIndex:0,
			endIndex:0,
			year:0,
			list:[],
			usercacheKey:"hr-train-instYearPlan-title2-user-cache",
			evaluateUserCacheKey:"hr-train-instYearPlan-title2-evaluateUser-cache"
		};
	},
	methods: {
		verifyEvaluateUser(){
			let _that = this
			if(!_that.$refs.autoform.formdata.evaluatePostId){
				let title = _that.$refs.autoform.getEditorTitle("title2","evaluatePostId")
				_that.om.toast("请先选择"+title)
				return false
			}else{
				_that.$refs.autoform.changeEditor("title2","evaluateUserId",{
					link:"../../public/search/search",
					"linkParam":{
						other:JSON.stringify({
							posId:_that.$refs.autoform.formdata.evaluatePostId
						}),
						url:"orgInstPosUserOfficial/findUserModelByPos",
						initload:true,
						cachename:_that.evaluateUserCacheKey,
						namefield:'username'
					}
				})
			}
			return true
		},
		verifyUser(){
			let _that = this
			if(!_that.$refs.autoform.formdata.postId){
				let title = _that.$refs.autoform.getEditorTitle("title2","postId")
				_that.om.toast("请先选择"+title)
				return false
			}else{
				_that.$refs.autoform.changeEditor("title2","userId",{
					link:"../../public/search/search",
					"linkParam":{
						other:JSON.stringify({
							posId:_that.$refs.autoform.formdata.postId
						}),
						url:"orgInstPosUserOfficial/findUserModelByPos",
						initload:true,
						cachename:_that.usercacheKey,
						namefield:'username'
					}
				})
			}
			return true
		},
		verifyTeacher(id,name){
			let _that = this
			_that.om.request(
				"trainYearPlan/verifyTeacher",
				{
					userId:id,
					relevantAbilityIdStr:''
				},
				'POST'
			).then(res=>{
				if(!res.value.flag){
					confirmModal(_that,'提示信息',res.value.confirmMsg,()=>{
						_that.$refs.autoform.formdata.teacherId = id
						_that.$refs.autoform.formdata.teacherName = name
					},()=>{
						
					})
				}
			})
		},
		getMonthList(){
			let _that  = this
			_that.om.request(
				"dictZero/getComboTree",
				{
					code:"38283"
				}
			).then(res=>{
				let monthList = []
				res.forEach((item,index)=>{
					let obj = JSON.parse(JSON.stringify(item))
					obj.index = index * 4
					delete obj.children
					monthList.push(obj)
					if(item.children){
						item.children.forEach((sub,num)=>{
							let obj1 = JSON.parse(JSON.stringify(sub))
							obj1.index = index * 4 + num + 1
							delete obj1.children
							monthList.push(obj1)
						})
					}
				})
				_that.$refs.autoform.changeEditor("title2","startDateObjId",{
					link:"../../public/pageSearch/pageSearch",
					"linkParam":{
						other:JSON.stringify({
						}),
						url:"dictZero/getComboTree",
						namefield:"text",
						defaultData:JSON.stringify(monthList),
						initload:true,
						cachename:_that.monthCacheKey,
					}
				})
				_that.$refs.autoform.changeEditor("title2","endDateObjId",{
					link:"../../public/pageSearch/pageSearch",
					"linkParam":{
						other:JSON.stringify({
						}),
						url:"dictZero/getComboTree",
						namefield:"text",
						defaultData:JSON.stringify(monthList),
						initload:true,
						cachename:_that.monthCacheKey1,
					}
				})
				
				
			})
		},
		initDatas(){
			let _that = this
			for(let index in _that.datas){
				if(_that.index === Number(index)){
					let arr = (_that.formdata.relationNameStr).split( ',' )
					arr.map(e=>{
						for(let item of _that.datas){
							if(item.orderNum === e){
								_that.relationList.push(item)
							}
						}
					})
				}
			}
		},
		initLoad() {
			let _that = this;
			changeEditor(_that.autoformdata[0].editors,"scoreNum","max",10)
			changeEditor(_that.autoformdata[0].editors,"scoreNum","min",0)
			changeEditor(_that.autoformdata[0].editors,"teacherId","isSearchUser",true)
			changeEditor(_that.autoformdata[0].editors,"teacherId",{
				link:'../../public/searchUser/searchUser',
				"linkParam":{
					cachename: _that.teacherCacheKey,
					isSingle:true
				}
			})
			
			changeEditor(
				_that.autoformdata[0].editors,
				'projectTypeId',
				{
					link: '/pages/public/pageSearch/pageSearch',
					linkParam: {
						other: JSON.stringify({
							code:"38562"
						}),
						namefield:"dicname",
						searchfield:"q",
						initload:true,
						url:"tsysdict/getCombobox",
						cachename: _that.projectCacheKey
					}
				},
				null
			);
			changeEditor(_that.autoformdata[0].editors,"evaluatePostId",{
				link:"../../public/searchUser/searchUser",
				"linkParam": {
					cachename:_that.evaluateUserPostCacheKey,
					isSingle:true,
					searchPos:true,
					label2:'posInstUserName',
					showPeopleNum:false,
					tips:'岗位选择(可选择所有人员岗位)'
				}
			},null,_that.autoformdata)
			changeEditor(_that.autoformdata[0].editors, 'startTime', 'endField', 'endTime');
			changeEditor(_that.autoformdata[0].editors, 'endTime', 'startField', 'startTime');
			//相关知识能力
			changeEditor(
			_that.autoformdata[0].editors,
				'trainTypeId',
				{
					// link: '/pages/public/search/search',
					linkParam: {
						other: JSON.stringify({
							code: '38404'
						}),
						namefield: 'dicname',
						likefield: 'dicname',
						initload: true,
						url: 'tsysdict/getCombobox',
						cachename: _that.trainTypeCacheKey
					}
				},
				null)
			changeEditor(
				'title2','relevantAbilityIdStr',
				{
					// link: '/pages/public/search/search',
					linkParam: {
						other: JSON.stringify({
							code: '35905'
						}),
						namefield: 'text',
						likefield: 'text',
						initload: true,
						url: 'trainYearPlan/getTrainSubjectItemList',
						cachename: _that.relevantAbilityCacheKey
					}
				},null,_that.autoformdata);
			//负责岗位
			changeEditor(
				_that.autoformdata[0].editors,
				'postId',
				{
					link: '/pages/public/searchUser/searchUser',
					linkParam: {
						cachename:_that.postCacheKey,
						isSingle:true,
						searchPos:true,
						label2:'posInstUserName',
						showPeopleNum:false,
						tips:'岗位选择(可选择所有人员岗位)'
					}
				},
				null
			);
			changeEditor("title2","scoreNum",{
				defaultName:10
			},null,_that.autoformdata)
			
			changeEditor(_that.autoformdata[0].editors,"relationIdStr",
				{
				'linkParam': {
					other: JSON.stringify({
						trainYearPlanId:_that.trainYearPlanId,
						year:_that.year
					}),
					url:"trainYearPlan/getCombotree",
					namefield: 'text',
					initload: true,
					cachename: _that.relationCacheKey
				}
			},null)
			_that.initDatas()
			_that.relationList = []
			_that.$nextTick(function(){
				_that.getMonthList()
			})
		}
	},
	onLoad(e) {
		let _that = this;
		this.year = e.year
		this.trainYearPlanId = e.trainYearPlanId
		subOnLoad(_that, e, function() {
			if (e.configDataCacheKey) {
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						//if (res.data && res.data instanceof Array) {
						if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
							_that.autoformdata = res.data;
							_that.formdata.userPostName = _that.formdata.userName
							Object.assign(_that.initValue, _that.formdata);
							_that.autoformdata[0].order = _that.order;
							_that.initLoad();
							// _that.om.request(
							// ).then(res=>{
							// 	debugger
							// 	res.map(e=>{
							// 		for(let item of _that.datas){
							// 			if(e.attributes.orderNum === item.orderNum){
							// 				item.text = e.text
							// 			}
							// 		}
							// 	})
							// })
						}
						_that.$nextTick(function(){
							if(_that.$refs.autoform.formdata.trainInstDataList == ''){
								_that.$refs.autoform.formdata.trainInstDataList = []
							}
							let arr = [
								_that.om.request(
									"tsysdict/getCombobox",
									{
										code:'38404'
									}),
								_that.om.request(
									"tsysdict/getCombobox",
									{
										code:'38880'
									}),
								_that.om.request(
									"tsysdict/getCombobox",
									{
										code:'37967'
									})	
							]
							Promise.all(arr).then(res=>{
								_that.$refs.autoform.changeEditor('title2',"trainTypeId",'data',configSelectData(res[0],'id','dicname'))
								_that.$refs.autoform.initSelectIndex('title2',"trainTypeId")
								_that.$refs.autoform.changeEditor('title2',"trainModeId",'data',configSelectData(res[1],'id','dicname'))
								_that.$refs.autoform.initSelectIndex('title2',"trainModeId")
								_that.$refs.autoform.changeEditor('title2',"placeId",'data',configSelectData(res[2],'id','dicname'))
								_that.$refs.autoform.initSelectIndex('title2',"placeId")
								if(_that.$refs.autoform.formdata.relationIdStr){
									let ids = _that.$refs.autoform.formdata.relationIdStr.split(',')
									let arr = []
									for(let id of ids){
										let obj = {
											id:id
										}
										arr.push(obj)
									}
									_that.$refs.autoform.changeEditor("title2","relationIdStr",{
										initSelect:arr
									})
								}
							})
						})
					}
				});
			}
		});
	},
	onShow() {
		let _that = this;
		if (_that.$refs.autoform) {
			uni.getStorage({
				key: _that.relevantAbilityCacheKey,
				success: function(res) {
					if (res.data) {
						let d = res.data;
						if (d && d.length > 0) {
							var idStr = joinObjStr(d, 'id');
							var nameStr = joinObjStr(d, 'text');
							var formdata = _that.$refs.autoform.formdata;
							formdata.relevantAbilityIdStr = idStr;
							formdata.relevantAbilityNameStr = nameStr;
							_that.$refs.autoform.changeEditor("title2","relevantAbilityIdStr",{
								initSelect:d
							})
						}
						uni.removeStorage({
							key: _that.relevantAbilityCacheKey
						});
					}
				}
			});
			uni.getStorage({
				key:_that.relationCacheKey,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.relationNameStr = d.map(e=>e.text).join(",")
						formdata.relationIdStr = d.map(e=>e.id).join(",")
						_that.initDatas()
						_that.$refs.autoform.changeEditor("title2","relationIdStr",{
							initSelect:d
						})
						_that.relationList = []
						uni.removeStorage({
							key:_that.relationCacheKey
						})
					}
				}
			})
			uni.getStorage({
				key:_that.projectCacheKey,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.projectTypeId = d.id
						formdata.projectTypeName = d.dicname
						uni.removeStorage({
							key:_that.projectCacheKey
						})
					}
				}
			})
			uni.getStorage({
				key: _that.postCacheKey,
				success: function(res) {
					if (res.data) {
						let d = res.data[0];
						var formdata = _that.$refs.autoform.formdata;
						formdata.postId = d.id;
						formdata.postName = d.posName
						formdata.instId = d.instId
						formdata.instName = d.instName
						_that.$refs.autoform.clear("title2","userId")
						uni.removeStorage({
							key: _that.postCacheKey
						});
					}
				}
			});
			uni.getStorage({
				key: _that.teacherCacheKey,
				success: function(res) {
					if (res.data) {
						let d = res.data[0];
						var formdata = _that.$refs.autoform.formdata;
						_that.verifyTeacher(d.id,d.username)
						uni.removeStorage({
							key: _that.teacherCacheKey
						});
					}
				}
			});
			uni.getStorage({
				key: _that.trainTypeCacheKey,
				success: function(res) {
					if (res.data) {
						let d = res.data;
						var formdata = _that.$refs.autoform.formdata;
						formdata.trainTypeId = d.id
						formdata.trainTypeName = d.dicname
						uni.removeStorage({
							key: _that.trainTypeCacheKey
						});
					}
				}
			});
			uni.getStorage({
				key: _that.evaluateUserPostCacheKey,
				success: function(res) {
					if (res.data) {
						let d = res.data[0];
						var formdata = _that.$refs.autoform.formdata;
						let title1 = _that.$refs.autoform.getEditorTitle("title1","postId")
						let title2 = _that.$refs.autoform.getEditorTitle("title1","evaluatePostId")
						if(_that.$refs.autoform.formdata.postId == d.id){
							_that.om.toast(+"作为"+title1+"，不能再选择为"+title2+"，请重新选择！",3000)
						}else{
							formdata.evaluatePostId =d.id
							formdata.evaluatePostName = d.posName
						}
						_that.$refs.autoform.clear("title2","evaluatePostId")
						uni.removeStorage({
							key: _that.evaluateUserPostCacheKey
						});
					}
				}
			});
			uni.getStorage({
				key:_that.monthCacheKey,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.startDateObjId = d.id
						formdata.startDateObjName = d.text
						switch(_that.$refs.autoform.formdata.endDateObjName.length){
							case 2:
							if(d.attributes.dictZero.referNum > Number(_that.$refs.autoform.formdata.endDateObjName.substring(0,1))){
								_that.$refs.autoform.clear("title2","endDateObjId")
							}
							break
							case 3:
							if(d.attributes.dictZero.referNum >  Number(_that.$refs.autoform.formdata.endDateObjName.substring(0,2))){
								_that.$refs.autoform.clear("title2","endDateObjId")
							}
							break;
							case 4:
							if(d.attributes.dictZero.referNum > Number(_that.$refs.autoform.formdata.endDateObjName.substring(0,1))){
								_that.$refs.autoform.clear("title2","endDateObjId")
							}else if(d.attributes.dictZero.referNum ===  Number(_that.$refs.autoform.formdata.endDateObjName.substring(0,1))){
								if(d.attributes.dictZero.dicname.substr(2,1) === "中"){
									if(_that.$refs.autoform.formdata.endDateObjName.substr(2,1) ==="上"){
										_that.$refs.autoform.clear("title2","endDateObjId")
									}
								}else if(d.attributes.dictZero.dicname.substr(2,1) === "下"){
									if(_that.$refs.autoform.formdata.endDateObjName.substr(2,1) ==="上" || _that.$refs.autoform.formdata.endDateObjName.substr(2,1) ==="中"){
										_that.$refs.autoform.clear("title2","endDateObjId")
									}
								}
							
							}
							break;
							case 5:
							if(d.attributes.dictZero.referNum >  Number(_that.$refs.autoform.formdata.endDateObjName.substring(0,2))){
								_that.$refs.autoform.clear("title2","endDateObjId")
							}else if(d.attributes.dictZero.referNum ===  Number(_that.$refs.autoform.formdata.endDateObjName.substring(0,2))){
								if(d.attributes.dictZero.dicname.substr(3,1) === "中"){
									if(_that.$refs.autoform.formdata.endDateObjName.substr(3,1) ==="上"){
										_that.$refs.autoform.clear("title2","endDateObjId")
									}
								}else if(d.attributes.dictZero.dicname.substr(3,1) === "下"){
									if(_that.$refs.autoform.formdata.endDateObjName.substr(3,1) ==="上" || _that.$refs.autoform.formdata.endDateObjName.substr(3,1) ==="中"){
										_that.$refs.autoform.clear("title2","endDateObjId")
									}
								}
							
							}
							break;
						}
						uni.removeStorage({
							key:_that.monthCacheKey
						})
					}
				}
			})
			uni.getStorage({
				key:_that.monthCacheKey1,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.endDateObjId = d.id
						formdata.endDateObjName = d.text
						switch(_that.$refs.autoform.formdata.startDateObjName.length){
							case 2:
							if(d.attributes.dictZero.referNum < _that.$refs.autoform.formdata.startDateObjName.substring(0,1)){
								_that.$refs.autoform.clear("title2","startDateObjId")
							}
							break
							case 3:
							if(d.attributes.dictZero.referNum < _that.$refs.autoform.formdata.startDateObjName.substring(0,2)){
								_that.$refs.autoform.clear("title2","startDateObjId")
							}
							break;
							case 4:
							if(d.attributes.dictZero.referNum < Number(_that.$refs.autoform.formdata.startDateObjName.substring(0,1))){
								_that.$refs.autoform.clear("title2","startDateObjId")
							}else if(d.attributes.dictZero.referNum ===  Number(_that.$refs.autoform.formdata.startDateObjName.substring(0,1))){
								if(d.attributes.dictZero.dicname.substr(2,1) === "中"){
									if(_that.$refs.autoform.formdata.startDateObjName.substr(2,1) ==="下"){
										_that.$refs.autoform.clear("title2","startDateObjId")
									}
								}else if(d.attributes.dictZero.dicname.substr(2,1) === "上"){
									if(_that.$refs.autoform.formdata.startDateObjName.substr(2,1) ==="中" || _that.$refs.autoform.formdata.startDateObjName.substr(2,1) ==="下"){
										_that.$refs.autoform.clear("title2","startDateObjId")
									}
								}
							
							}
							break;
							case 5:
							if(d.attributes.dictZero.referNum < _that.$refs.autoform.formdata.startDateObjName.substring(0,2)){
								_that.$refs.autoform.clear("title2","startDateObjId")
							}else if(d.attributes.dictZero.referNum ===  Number(_that.$refs.autoform.formdata.startDateObjName.substring(0,2))){
								if(d.attributes.dictZero.dicname.substr(3,1) === "中"){
									if(_that.$refs.autoform.formdata.startDateObjName.substr(3,1) ==="下"){
										_that.$refs.autoform.clear("title2","startDateObjId")
									}
								}else if(d.attributes.dictZero.dicname.substr(3,1) === "上"){
									if(_that.$refs.autoform.formdata.startDateObjName.substr(3,1) ==="中" || _that.$refs.autoform.formdata.startDateObjName.substr(3,1) ==="下"){
										_that.$refs.autoform.clear("title2","startDateObjId")
									}
								}
							
							}
							break;
						}
						uni.removeStorage({
							key:_that.monthCacheKey1
						})
					}
				}
			})
			uni.getStorage({
				key:_that.usercacheKey,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.userId = d.id
						formdata.userName = d.username
						uni.removeStorage({
							key:_that.usercacheKey
						})
					}
				}
			})
			uni.getStorage({
				key:_that.evaluateUserCacheKey,
				success:(res)=>{
					if(res.data){
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						formdata.evaluateUserId = d.id
						formdata.evaluateUserName = d.username
						uni.removeStorage({
							key:_that.evaluateUserCacheKey
						})
					}
				}
			})
		}
	}
};
</script>
<style></style>
